﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Device.Location;
using System.IO.IsolatedStorage;
using System.IO;
namespace ThessalonikiGuide
{
    public partial class MainPage
    {

        private static bool hasSeenIntro;
        static string LandingBitFileName = "l.b";
        /// <summary>Will return false only the first time a user ever runs this.
        /// Everytime thereafter, a placeholder file will have been written to disk
        /// and will trigger a value of true.</summary>
        public static bool HasUserSeenIntro()
        {
            if (hasSeenIntro) return true;

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(LandingBitFileName))
                {
                    // just write a placeholder file one byte long so we know they've landed before
                    using (var stream = store.OpenFile(LandingBitFileName, FileMode.Create))
                    {
                        stream.Write(new byte[] { 1 }, 0, 1);
                    }
                    return false;
                }

                hasSeenIntro = true;
                return true;
            }
        }

        private ItemFlyInAndOutAnimations _flyOutAnimation = null;
        public static List<List<String>> Tiles = new List<List<string>>{
         new List<string>{"Πιάτσες Ταξί", "Χώροι Στάθμευσης","Δημόσιοι Χώροι","Υγεία","Δημόσια Τάξη", "Βενζινάδικα"},
        new List<string> {"Ξενοδοχεία", "Αξιοθέατα", "Θρησκεία","Πολιτισμός","Πρεσβείες - Προξενεία"},
        new List<string> { "Εκπαίδευση", "ΚΑΠΗ", "ΕΥΑΘ", "Διοικητικές Υπηρεσίες"} ,
        new List<string> { "Δημόσιες Υπηρεσίες", "4η Εφορεία","9η Εφορεία", "ΙΣΤη Εφορεία"},
        };
        public static List<List<String>> Names = new List<List<string>>{
         new List<string>{"ΠΙΑΤΣΕΣ ΤΑΞΙ", "ΧΩΡΟΙ ΣΤΑΘΜΕΥΣΗΣ","ΔΗΜΟΣΙΟΙ ΧΩΡΟΙ","ΥΓΕΙΑ","ΔΗΜΟΣΙΑ ΤΑΞΗ"},
        new List<string> {"ΞΕΝΟΔΟΧΕΙΑ", "ΑΞΙΟΘΕΑΤΑ", "ΘΡΗΣΚΕΙΑ","ΠΟΛΙΤΙΣΜΟΣ","ΠΡΕΣΒΕΙΕΣ - ΠΡΟΞΕΝΕΙΑ"},
        new List<string> { "ΕΚΠΑΙΔΕΥΣΗ", "ΚΕΝΤΡΑ ΑΝΟΙΚΤΗΣ ΠΡΟΣΤΑΣΙΑΣ ΗΛΙΚΙΩΜΕΝΩΝ", "ΕΥΑΘ", "ΔΙΟΙΚΗΤΙΚΕΣ ΥΠΗΡΕΣΙΕΣ"} ,
        new List<string> { "ΔΗΜΟΣΙΕΣ ΥΠΗΡΕΣΙΕΣ", "4η ΕΦΟΡΕΙΑ","9η ΕΦΟΡΕΙΑ", "ΙΣΤη ΕΦΟΡΕΙΑ"},
        };
        int _i;
        int _j;
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (_flyOutAnimation != null)
            {
                _flyOutAnimation.ItemFlyIn();
            }
            base.OnNavigatedTo(e);
        
            if (!HasUserSeenIntro())
            {
                NavigationService.Navigate(new Uri("/Privacy.xaml", UriKind.Relative));
                return;
            }
            //watcher = new GeoCoordinateWatcher();
            //watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            //if (IsolatedStorageSettings.ApplicationSettings["canuselocation"] as bool? == true)
            //{
            //    watcher.Start();
            //}
            //else
            //{
            //    MessageBox.Show("Η εφαρμογή αυτή χρειάζεται πρόσβαση στην τοποθεσία σας για να λειτουργήσει σωστα.");
            //    NavigationService.Navigate(new Uri("/Privacy.xaml?red=1",UriKind.Relative));
            //}
            foreach (PanoramaItem item in panorama.Items)
            {
                _i = 0;
                _j = 0;
                var scroll = new ScrollViewer();
                var grid = new Grid();
                for (var x = 0; x < 3; x++)
                {
                    grid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto});
                }
                for (var x = 0; x < 2; x++)
                {
                    grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});
                }
                foreach (var tile in Tiles[panorama.Items.IndexOf(item)].Select(Tile))
                {
                    grid.Children.Add(tile);
                    Grid.SetColumn(tile, _i);
                    Grid.SetRow(tile, _j);
                    if (_i == 1)
                    {
                        _i = 0;
                        _j++;
                    }
                    else
                    {
                        _i++;
                    }
                }
               
                scroll.Content = grid;
                item.Content = scroll;
            }
        }

        //void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        //{
        //    watcher.Stop();
        //    if (e.Position.Location.GetDistanceTo(new GeoCoordinate { Latitude = 40.626369, Longitude = 22.948428 }) > 10000)
        //    {
        //        if (MessageBox.Show("Δεν βρίσκεστε στην Θεσσαλονίκη. Παρακαλώ πηγαίνετε στην Θεσσαλονίκη.", "NotInThessalonikiError", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        //        {
        //            NavigationService.Navigate(new Uri("/GPS.xaml?coordinates=22.948428,40.626369&name=Θεσσαλονίκη", UriKind.Relative));
        //        }
        //    }
        //}
        
        private HubTile Tile(string name)
        {
            var tile = new HubTile
                           {
                               Title = name,
                               Style = (Style)Resources["HubTileStyle0"],
                               Margin = new Thickness {Left = 20, Bottom = 20},
                               Source = new BitmapImage(new Uri("/Images/" + name + ".jpg", UriKind.Relative)),
                           };
            tile.Tap += delegate
            {
                _flyOutAnimation = new ItemFlyInAndOutAnimations();
                _flyOutAnimation.ItemFlyOut(tile, () =>
                {
                    NavigationService.Navigate(new Uri("/subcats.xaml?categ=" + name, UriKind.Relative));
                });
            };
            return tile;
        }
    }
}
